#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    Display *display = XOpenDisplay(NULL);
    if (!display) {
        fprintf(stderr, "Cannot open display\n");
        exit(1);
    }

    int screen_num = DefaultScreen(display);
    Window root = RootWindow(display, screen_num);

    // Create a simple window
    Window win = XCreateSimpleWindow(
        display,
        root,
        10,
        10,
        200,
        200,
        1,
        BlackPixel(display, screen_num),
        WhitePixel(display, screen_num)
    );

    XSelectInput(display, win, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | StructureNotifyMask | FocusChangeMask | PropertyChangeMask | ExposureMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask);
    // Map the window to the screen
    XMapWindow(display, win);

    // Wait for the map event before querying the window size
    XEvent event;
#if 0
    do {
        XNextEvent(display, &event);
    } while (event.type != MapNotify || event.xmap.event != win);
#endif

    // Get window attributes
    XWindowAttributes attr;
    if (XGetWindowAttributes(display, win, &attr)) {
        printf("Window ID: %lu\n", win);
        printf("Width: %d\n", attr.width);
        printf("Height: %d\n", attr.height);
        printf("Border Width: %d\n", attr.border_width);
        printf("Depth: %d\n", attr.depth);
    } else {
        fprintf(stderr, "Failed to get window attributes\n");
    }

    // Enter an infinite loop to keep the application running
    while (1) {
        XNextEvent(display, &event);
        switch(event.type) {
            case Expose:
                // Redraw the window here if necessary
                break;
            case KeyPress:
                // Handle key press events here
                XCloseDisplay(display);
                return 0;
            default:
                break;
        }
    }

    XCloseDisplay(display);
    return 0;
}




